package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.DeviceDataDto;
import com.zzyl.nursing.dto.HandleDataDto;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 报警数据Service业务层处理
 *
 * @author alexis
 * @date 2025-01-15
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return alertDataMapper.selectById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return alertDataMapper.insert(alertData);
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return alertDataMapper.updateById(alertData);
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return alertDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return alertDataMapper.deleteById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param deviceDataDto
     * @return
     */
    @Override
    public TableDataInfo<AlertData> listByUserID(DeviceDataDto deviceDataDto) {
        Page<AlertData> pageParam = new Page<>(deviceDataDto.getPageNum(), deviceDataDto.getPageSize());

        //获取登录人id
        Long userId = SecurityUtils.getUserId();
        //执行分页查询
        Page<AlertData> page = page(pageParam, Wrappers.<AlertData>lambdaQuery()
                .eq(StringUtils.isNotEmpty(deviceDataDto.getDeviceName()), AlertData::getDeviceName, deviceDataDto.getDeviceName())
                .between((ObjectUtil.isNotEmpty(deviceDataDto.getStartTime()) && ObjectUtil.isNotEmpty(deviceDataDto.getEndTime())),
                        AlertData::getCreateTime, deviceDataDto.getStartTime(), deviceDataDto.getEndTime())
                .eq(AlertData::getUserId,userId));

        //封装数据
        TableDataInfo<AlertData> listTableDataInfo = new TableDataInfo<>();
        listTableDataInfo.setCode(200);
        listTableDataInfo.setMsg("查询成功");
        listTableDataInfo.setRows(page.getRecords());
        listTableDataInfo.setTotal(page.getTotal());
        return listTableDataInfo;
    }

    /**
     * 处理设备报警数据
     *
     * @param handleDataDto
     */
    @Override
    public void handleAlertData(HandleDataDto handleDataDto) {

        //查询通知人的id集合
        AlertData data = getById(handleDataDto.getId());
        List<AlertData> alertDataList = list(Wrappers.<AlertData>lambdaQuery()
                .eq(AlertData::getIotId, data.getIotId())
                .eq(AlertData::getProductKey, data.getProductKey())
                .eq(AlertData::getFunctionId, data.getFunctionId())
                .eq(AlertData::getCreateTime, data.getCreateTime()));
        alertDataList.forEach(ad -> {
            //处理结果
            ad.setProcessingResult(handleDataDto.getProcessingResult());
            //处理时间
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            ad.setProcessingTime(LocalDateTime.parse(handleDataDto.getProcessingTime(), formatter));
            //状态
            ad.setStatus(1);
            //处理人id
            Long userId = SecurityUtils.getUserId();
            ad.setProcessorId(userId);
            //查询处理人姓名
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            ad.setProcessorName(sysUser.getNickName());
        });
        //批量更新报警数据
        updateBatchById(alertDataList);
    }
}
